package cn.fl.system.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUsrDatAuthRService;
import cn.fl.system.vo.SysUsrDatAuthRVO;
import net.sf.json.JSONArray;

/**
 * @Description: 系统用户数据权限关系表 控制层
 */
@Controller
@RequestMapping("/system/sysUsrDatAuthR")
public class SysUsrDatAuthRController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysUsrDatAuthRController.class);

	/**
	 * SysUsrDatAuthR 服务层BEAN
	 */
	@Autowired
	private ISysUsrDatAuthRService sysUsrDatAuthRService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toListPage", "加载用户数据权限关系列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysUsrDatAuthR/sysSysUsrDatAuthRList");
		return view;
	}

	
	/**
	 * 新增 数据权限关联关系
	 */
	@RequestMapping(value = "/addSysUsrDatAuthR.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addSysUsrDatAuthR(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		if(StringUtils.isBlank(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addSysUsrDatAuthR", "新增数据权限关系,组织架构ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String usrIdArr = request.getParameter("usrIdArr");
		if(StringUtils.isBlank(usrIdArr)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_USR_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addSysUsrDatAuthR", "新增数据权限关系,用户ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		JSONArray jsonArray = JSONArray.fromObject(usrIdArr);
		List<SysUsrDatAuthRVO> sysUsrDatAuthRVOList = new ArrayList<SysUsrDatAuthRVO>();
		for (int i = 0; i < jsonArray.size(); i++) {
			Object object = jsonArray.getJSONObject(i).get("usrId");
			Long usrId = Long.valueOf(object.toString());
			Integer  isChg = (Integer) jsonArray.getJSONObject(i).get("isChg");
			SysUsrDatAuthRVO sysUsrDatAuthRVO = new SysUsrDatAuthRVO();
			sysUsrDatAuthRVO.setIsChg(isChg);
			sysUsrDatAuthRVO.setOrgId(Long.valueOf(orgId));
			sysUsrDatAuthRVO.setUsrId(usrId);
			sysUsrDatAuthRVOList.add(sysUsrDatAuthRVO);
		}
	
		try {
			
			boolean success = this.sysUsrDatAuthRService.addSysUsrDatAuthR(sysUsrDatAuthRVOList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
				BaseLogUtils.info(logger, "addSysUsrDatAuthR", "新增数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "addSysUsrDatAuthR", "新增数据权限关系成功..orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addSysUsrDatAuthR", "新增数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addSysUsrDatAuthR", "新增数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	/**
	 * 修改 数据权限关联关系
	 */
	@RequestMapping(value = "/updateSysUsrDatAuthR.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object updateSysUsrDatAuthR(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isBlank(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "updateSysUsrDatAuthR", "修改数据权限关系,组织架构ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String usrIdArr = request.getParameter("usrIdArr");
		if(StringUtils.isBlank(usrIdArr)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_USR_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "updateSysUsrDatAuthR", "修改数据权限关系,用户ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		JSONArray jsonArray = JSONArray.fromObject(usrIdArr);
		List<SysUsrDatAuthRVO> sysUsrDatAuthRVOList = new ArrayList<SysUsrDatAuthRVO>();
		for (int i = 0; i < jsonArray.size(); i++) {
			Object object = jsonArray.getJSONObject(i).get("usrId");
			Long usrId = Long.valueOf(object.toString());
			Integer  isChg = (Integer) jsonArray.getJSONObject(i).get("isChg");
			SysUsrDatAuthRVO sysUsrDatAuthRVO = new SysUsrDatAuthRVO();
			sysUsrDatAuthRVO.setIsChg(isChg);
			sysUsrDatAuthRVO.setOrgId(Long.valueOf(orgId));
			sysUsrDatAuthRVO.setUsrId(usrId);
			sysUsrDatAuthRVOList.add(sysUsrDatAuthRVO);
		}
		
		try {
			
			boolean success = this.sysUsrDatAuthRService.modifySysUsrDatAuthRVO(sysUsrDatAuthRVOList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_UPDATE_FAIL);
				BaseLogUtils.info(logger, "updateSysUsrDatAuthR", "修改数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "updateSysUsrDatAuthR", "修改数据权限关系成功.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "updateSysUsrDatAuthR", "修改数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "updateSysUsrDatAuthR", "修改数据权限关系失败,系统错误.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}

	/**
	 * 移除数据权限关联关系
	 */
	@RequestMapping(value = "/removeSysUsrDatAuthR.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeSysUsrDatAuthR(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isBlank(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeSysUsrDatAuthR", "删除数据权限关系,组织架构ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String[] usrIdArr = request.getParameterValues("usrIdArr[]");
		if(usrIdArr==null||usrIdArr.length<=0){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_USR_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeSysUsrDatAuthR", "删除数据权限关系,用户ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<String> usrIdList = new ArrayList<String>();
		Collections.addAll(usrIdList, usrIdArr);
		List<Long> usrIds = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(usrIdList)){
			
			for (String idStr : usrIdList) {
				usrIds.add(Long.valueOf(idStr));
			}
		}
		try {
			boolean success = this.sysUsrDatAuthRService.removeSysUsrDatAuthR(Long.valueOf(orgId), usrIds);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeSysUsrDatAuthR", "删除数据权限关系失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeSysUsrDatAuthR", "删除数据权限关系成功.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeSysUsrDatAuthR", "删除数据权限关系失败.orgId:"+orgId+".usrId:" + usrIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeSysUsrDatAuthR", "删除数据权限关系失败,系统错误..orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr)+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	
	
	
	
}

